<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>lqr</title>
  </head>
  <body bgcolor="#FFFFFF">
    <center>Scilab Function</center>
    <div align="right">Last update : April 1993</div>
    <p>
      <b>lqr</b> -  LQ compensator (full state)  </p>
    <h3>
      <font color="blue">Calling Sequence</font>
    </h3>
    <dl>
      <dd>
        <tt>[K,X]=lqr(P12)  </tt>
      </dd>
    </dl>
    <h3>
      <font color="blue">Parameters</font>
    </h3>
    <ul>
      <li>
        <tt>
          <b>P12</b>
        </tt>: <tt>
          <b>syslin</b>
        </tt> list (state-space linear system)</li>
      <li>
        <tt>
          <b>K,X</b>
        </tt>: two real matrices</li>
    </ul>
    <h3>
      <font color="blue">Description</font>
    </h3>
    <p>
      <tt>
        <b>lqr</b>
      </tt>  computes the linear optimal LQ full-state gain
    for the plant <tt>
        <b>P12=[A,B2,C1,D12]</b>
      </tt> in continuous or
    discrete time.</p>
    <p>
      <tt>
        <b>P12</b>
      </tt> is a <tt>
        <b>syslin</b>
      </tt> list (e.g. <tt>
        <b>P12=syslin('c',A,B2,C1,D12)</b>
      </tt>).</p>
    <p>
    The cost function is l2-norm of <tt>
        <b>z'*z</b>
      </tt> with <tt>
        <b>z=C1 x + D12 u</b>
      </tt>
    i.e. <tt>
        <b>[x,u]' * BigQ * [x;u]</b>
      </tt> where</p>
    <pre>

      [C1' ]               [Q  S]
BigQ= [    ]  * [C1 D12] = [    ]
      [D12']               [S' R]
   
    </pre>
    <p>
    The gain <tt>
        <b>K</b>
      </tt> is such that <tt>
        <b>A + B2*K</b>
      </tt> is stable.</p>
    <p>
      <tt>
        <b>X</b>
      </tt> is the stabilizing solution of the Riccati equation.</p>
    <p>
    For a continuous plant:</p>
    <pre>

(A-B2*inv(R)*S')'*X+X*(A-B2*inv(R)*S')-X*B2*inv(R)*B2'*X+Q-S*inv(R)*S'=0
   
    </pre>
    <pre>

K=-inv(R)*(B2'*X+S)
   
    </pre>
    <p>
    For a discrete plant:</p>
    <pre>

X=A'*X*A-(A'*X*B2+C1'*D12)*pinv(B2'*X*B2+D12'*D12)*(B2'*X*A+D12'*C1)+C1'*C1;
   
    </pre>
    <pre>

K=-pinv(B2'*X*B2+D12'*D12)*(B2'*X*A+D12'*C1)
   
    </pre>
    <p>
    An equivalent form for <tt>
        <b>X</b>
      </tt> is</p>
    <pre>

X=Abar'*inv(inv(X)+B2*inv(r)*B2')*Abar+Qbar
   
    </pre>
    <p>
    with <tt>
        <b>Abar=A-B2*inv(R)*S'</b>
      </tt> and <tt>
        <b>Qbar=Q-S*inv(R)*S'</b>
      </tt>
    </p>
    <p>
    The 3-blocks matrix pencils associated with these Riccati equations are:</p>
    <pre>

               discrete                           continuous
   |I   0    0|   | A    0    B2|         |I   0   0|   | A    0    B2|
  z|0   A'   0| - |-Q    I    -S|        s|0   I   0| - |-Q   -A'   -S|
   |0   B2'  0|   | S'   0     R|         |0   0   0|   | S'  -B2'   R|
   
    </pre>
    <p>
    Caution: It is assumed that matrix R is non singular. In particular,
    the plant must be tall (number of outputs &gt;= number of inputs).</p>
    <h3>
      <font color="blue">Examples</font>
    </h3>
    <pre>

A=rand(2,2);B=rand(2,1);   //two states, one input
Q=diag([2,5]);R=2;     //Usual notations x'Qx + u'Ru
Big=sysdiag(Q,R);    //Now we calculate C1 and D12
[w,wp]=fullrf(Big);C1=wp(:,1:2);D12=wp(:,3:$);   //[C1,D12]'*[C1,D12]=Big
P=syslin('c',A,B,C1,D12);    //The plant (continuous-time)
[K,X]=lqr(P)
spec(A+B*K)    //check stability
norm(A'*X+X*A-X*B*inv(R)*B'*X+Q,1)  //Riccati check
P=syslin('d',A,B,C1,D12);    // Discrete time plant
[K,X]=lqr(P)     
spec(A+B*K)   //check stability
norm(A'*X*A-(A'*X*B)*pinv(B'*X*B+R)*(B'*X*A)+Q-X,1) //Riccati check
 
  </pre>
    <h3>
      <font color="blue">See Also</font>
    </h3>
    <p>
      <a href="lqe.htm">
        <tt>
          <b>lqe</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="../robust/gcare.htm">
        <tt>
          <b>gcare</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="../robust/leqr.htm">
        <tt>
          <b>leqr</b>
        </tt>
      </a>,&nbsp;&nbsp;</p>
    <h3>
      <font color="blue">Author</font>
    </h3>
    <p>F.D.;   </p>
  </body>
</html>
